--
-- Author: shineflag
-- Date: 2016-09-21 12:13:39
--


local skynet = require "skynet"
local log = require "log"

local service = {}

--创建一个service的模型
local function create_service_model( )
	return {
		reqsrv = nil,   --需要交互的其它 service
		command = {},   --接收其它server的消息处理函数
		info = nil,      --可以通过debug_console 监控的数据
		init = nil,      --初始化函数
	}

end

function service.init( mod )
	funcs = mod.command
	if mod.info then
		skynet.info_func(function()
			return mod.info
		end)
	end

	skynet.start(function ( ... )
		if mod.reqsrv then
			for _, name in pairs(mod.reqsrv) do
				service[name] = skynet.uniqueservice(name)
			end
		end

		if mod.init then
			mod.init()
		end

		skynet.dispatch("lua", function (_,_, cmd, ...)
			local f = funcs[cmd]
			if f then
				skynet.ret(skynet.pack(f(...)))
			else
				log("Unknown command : [%s]", cmd)
				skynet.response()(false)
			end
		end)
	end)

end

service.create_service_model = create_service_model

return service 